//
//  KSCollecItem.swift
//  ANOA
//
//  Created by 邱仙凯 on 2022/4/24.
//  Copyright © 2025 aowang. All rights reserved.
//  带collectionview 的一种KSFormHItem   群聊人员信息列表

import UIKit
import Kingfisher
struct KSCollParam {
    var iconUrl:String? = nil
    var iconImage:UIImage? = nil
    var name:String? = nil
}
let CollecWidth:CGFloat = (KSGlobal.screenW - 24)/5

class KSCollecItem: KSFormHItem,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    lazy var collectionView:UICollectionView = {
        // 1.创建layout
         let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: CollecWidth - 0.5, height: CollecWidth)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
        
        // 2.创建UICollectionView
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        collectionView.showsHorizontalScrollIndicator = false
        
        collectionView.register(KSCollecCell.self, forCellWithReuseIdentifier: KSCollecCell.identifier)
 
        return collectionView
    }()
    var dataArray = [KSCollParam]()
    convenience init(appearance: KSFormBaseAppear? = KSFormBaseAppear()) {
        self.init(title: "", appearance: appearance)
        
         
        collectionView.dataSource = self
        collectionView.delegate = self
        self.addSubview(collectionView)
         collectionView.snp.makeConstraints { (make) in
             make.right.equalTo(self)
             make.left.equalTo(self)
             make.top.equalTo(self).offset(3)
             make.bottom.equalTo(self).offset(-3)
        
        }
     }
    var collecSelected:((Int)->())?

    func setCollecDataSource(arr:[KSCollParam]) {
        self.dataArray = arr
        self.collectionView.reloadData()
    }
 
 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return self.dataArray.count
 
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KSCollecCell.identifier, for: indexPath as IndexPath) as! KSCollecCell
        let one = self.dataArray[indexPath.row]
       
        cell.title.text = one.name
        if one.iconImage.isSome {
            cell.img.image = one.iconImage!
         } else {
             if  let imageUrl = URL(string: one.iconUrl.unwrap) {
                 let resource = KF.ImageResource(downloadURL: imageUrl)
                 cell.img.kf.setImage(with: resource, placeholder: nil)
             }
 
        }
        
 
         return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if let clo = self.collecSelected {
            clo(indexPath.row)
        }
        
    }
}
class KSCollecCell: BadgeCollectionViewCell {
    
    var img: UIImageView = {
        let img = UIImageView()
        img.layer.masksToBounds = true
        img.layer.cornerRadius = 5
        return img
    }()
    var title: UILabel = {
        let lab :UILabel = UILabel()
        lab.textColor = UIColor(kshex:"#222222")
        lab.font = UIFont.systemFont(ofSize: 13)
        lab.textAlignment = .center
        lab.numberOfLines = 2
        return lab
    }()
 
    
    convenience required init(coder : NSCoder){
        self.init()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initUtil()
    }
    
    func initUtil() {
        self.contentView.backgroundColor = .clear
         
        self.contentView.addSubview(img)
        img.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.contentView)
            make.top.equalTo(self.contentView).offset(6)
            make.width.equalTo(self.contentView).multipliedBy(0.54)
            make.height.equalTo(img.snp.width)
      
        }
        self.contentView.addSubview(title)
        title.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.contentView)
            make.left.right.equalTo(self.contentView)
            make.bottom.equalTo(self.contentView).offset(-3)
        }
        
        
    }
}
